<?php
class Tourism_goods_attr_model extends CI_Model
{
    private $table = 'tourism_goods_attr';
    private $table_2 = 'tourism_goods_base';
    private $table_3 = 'user';

    public function total($param = array())
    {
        $this->db->select('b.*,u.alias_name,a.*');
        $this->db->from($this->table . ' as a');

        if (isset($param['good_name'])) {
            $this->db->like('b.good_name', trim($param['good_name']));
        }
        if (isset($param['alias_name'])) {
            $this->db->like('u.alias_name', trim($param['alias_name']));
        }
        if (isset($param['is_on_sale']) && $param['is_on_sale'] != 0) {
            $this->db->where('b.is_on_sale', (int)$param['is_on_sale']);
        }
        $this->db->join($this->table_2 . ' as b', 'a.goods_id = b.good_id');
        $this->db->join($this->table_3 . ' as u', 'u.uid = b.uid');

        return $this->db->count_all_results();
    }

    public function page_list($page_num, $num, $param = array())
    {
        $this->db->select('b.*,u.alias_name,a.*');
        $this->db->from($this->table . ' as a');

        if (isset($param['good_name'])) {
            $this->db->like('b.good_name', trim($param['good_name']));
        }
        if (isset($param['alias_name'])) {
            $this->db->like('u.alias_name', trim($param['alias_name']));
        }
        if (isset($param['is_on_sale']) && $param['is_on_sale'] != 0) {
            $this->db->where('b.is_on_sale', (int)$param['is_on_sale']);
        }
        $this->db->join($this->table_2 . ' as b', 'a.goods_id = b.good_id');
        $this->db->join($this->table_3 . ' as u', 'u.uid = b.uid');

        $this->db->order_by('b.good_id', 'desc');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }
    
    public function insertTourismGoodsAttr($goods_id, $postData)
    {
        $data = array(
            'goods_id'       => $goods_id,
            'market_price'   => !empty($postData['market_price']) ? $postData['market_price'] : 0,
            'shop_price'     => $postData['shop_price'],
            'supply_price'   => $postData['supply_price'],
            'goods_num'      => $postData['goods_num'],
            'weight'         => $postData['weight'],
            'swim'           => !empty($postData['swim']) ? $postData['swim'] : 0,
            'is_beizhu'      => $postData['is_beizhu'],
            'is_check'       => $postData['is_check'],
            'sale_count'     => $postData['sale_count'],
            'evaluate_count' => 0,
            'tour_count'     => 0,
            'sort'           => empty($postData['sort']) ? 0 : $postData['sort']
        );
        
        //商品规格 将属性规格保存
        if (empty($postData['attribute'])) {
            $data['attribute_key'] = '';
            $data['attribute_value'] = '';
        } else {
            $data['attribute_key']   = ',' . implode(',', $postData['attribute']) . ',';
            $data['attribute_value'] = $this->attribute($postData['attribute']);
        }
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    public function updateTourismGoodsAttr($goods_attr_id, $postData)
    {
        $data = array(
            'market_price'   => !empty($postData['market_price']) ? $postData['market_price'] : 0,
            'shop_price'     => $postData['shop_price'],
            'supply_price'   => $postData['supply_price'],
            'goods_num'      => $postData['goods_num'],
            'weight'         => $postData['weight'],
            'swim'           => !empty($postData['swim']) ? $postData['swim'] : 0,
            'is_check'       => $postData['is_check'],
            'is_beizhu'      => $postData['is_beizhu'],
            'sale_count'     => !empty($postData['sale_count']) ? $postData['sale_count'] : 0,
            'sort'           => empty($postData['sort']) ? 0 : $postData['sort'],
            'updated_at'     => date('Y-m-d H:i:s')
        );
    
        //商品规格 将属性规格保存
        if (empty($postData['attribute'])) {
            $data['attribute_key'] = '';
            $data['attribute_value'] = '';
        } else {
            $data['attribute_key']   = ',' . implode(',', $postData['attribute']) . ',';
            $data['attribute_value'] = $this->attribute($postData['attribute']);
        }
        $this->db->where('goods_attr_id', $goods_attr_id);
        return $this->db->update($this->table, $data);
    }
    
    /**
     * 获取商品的SKU对应的规格
     * author laona
     */
    public function attribute($postData = array())
    {
        $str = array();
        foreach ($postData as $item) {
            $str[] = $this->tourism_goods_category_spec->getSpecNameById($item);
        }
        if (!empty($str)) {
            $result = trim(implode(',', $str));
        } else {
            $result = '';
        }
        return $result;
    }

    /**
     * 设置产品审核状态
     * @param unknown $goods_attr_id
     * @param unknown $params
     */
    public function setIsCheckStatus($goods_attr_id, $is_check)
    {
        $data = array(
            'is_check'=>$is_check
        );
        if (is_array($goods_attr_id)) {
            $this->db->where_in('goods_attr_id', $goods_attr_id);
        } else {
            $this->db->where('goods_attr_id', $goods_attr_id);
        }
        return $this->db->update($this->table, $data);
    }
    
     /**
     * 逻辑删除
     * @param unknown $goods_id
     * @param unknown $is_on_sale
     */
    public function setIsLogicDelete($goods_attr_id, $isLogicDelete)
    {
        $data = array(
                'logic_delete'=>$isLogicDelete
        );
        if (is_array($goods_attr_id)) {
            $this->db->where_in('goods_attr_id', $goods_attr_id);
        } else {
            $this->db->where('goods_attr_id', $goods_attr_id);
        }
        return $this->db->update($this->table, $data);
    }
    
    public function getGoodsAttr($goods_attr_id)
    {
        $this->db->select('b.*,u.alias_name,a.*');
        $this->db->from($this->table . ' as a');
        $this->db->where('a.goods_attr_id', $goods_attr_id);
        $this->db->join($this->table_2 . ' as b', 'a.goods_id = b.good_id');
        $this->db->join($this->table_3 . ' as u', 'u.uid = b.uid');
        $this->db->limit(1);
        return $this->db->get()->row_array();
    }

    public function updateGoodsNum($goodsAttrId,$number){
        $this->db->set('goods_num', 'goods_num+'.$number, FALSE);
        $this->db->where('goods_attr_id', $goodsAttrId);
        return $this->db->update($this->table);
    }

}